package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.DepartmentBean;
import com.attendance.www.hr.beans.DepartmentPageBean;
import com.attendance.www.hr.dao.mapper.DepartmentExtMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.dao.mapper.EmployeeDepartmentMapper;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.entitys.EmployeeDepartment;
import com.attendance.www.hr.entitys.EmployeeDepartmentExample;
import com.attendance.www.hr.service.IDepartmentService;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
	@Resource
	private DepartmentMapper DepartmentDao;
	@Resource
	private DepartmentExtMapper DepartmentExtDao;
	@Resource
	private EmployeeCorporationMapper EmployeeCorporationDao;
	@Resource
	private EmployeeDepartmentMapper EmployeeDepartmentDao;
	

	/**
	 * 添加部门
	 */
	@Override
	public boolean saveDepartment(DepartmentBean departmentBean) {
		Department department=new Department();
		department.setDepartmentCode(departmentBean.getDepartmentCode());
		department.setDepartmentName(departmentBean.getDepartmentName());
		department.setDescription(departmentBean.getDescription());
		department.setCorporationId(departmentBean.getCorporationId());
		department.setOrgLevel(departmentBean.getOrgLevel());
		department.setFromDate(new Date());

		
		department.setParentDepartmentId(departmentBean.getParentDepartmentId());
		int record = DepartmentDao.insertSelective(department);
		
		if(departmentBean.getParentDepartmentId()!=null) {
			DepartmentExample example=new DepartmentExample();
			DepartmentExample.Criteria criteria = example.createCriteria();
			criteria.andDepartmentIdEqualTo(departmentBean.getParentDepartmentId());
			List<Department> list = DepartmentDao.selectByExample(example);
			department.setDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
		}		
		else {
			department.setDepartmentPath("-"+department.getDepartmentId()+"-");
		}
		DepartmentDao.updateByPrimaryKeySelective(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 获取部门列表带分页
	 */
	@Override
	public DepartmentPageBean getDepartmentList(DepartmentBean departmentBean, Integer startNum, Integer pageCount) {
		List<DepartmentBean> departmentBeanList=DepartmentExtDao.getDepartmentList(departmentBean, startNum, pageCount,true);
		Integer pageTotal=DepartmentExtDao.getDepartmentListPageTotal(departmentBean);
		DepartmentPageBean departmentPageBean =new DepartmentPageBean();
		departmentPageBean.setDepartmentBeanList(departmentBeanList);
		departmentPageBean.setPageTotal(pageTotal);
		return departmentPageBean;
	}
	@Override
	public List<DepartmentBean> getDepartmentPageList(DepartmentBean departmentBean) {
		List<DepartmentBean> departmentBeanList=DepartmentExtDao.getDepartmentList(departmentBean, 0, 0,false);
		return departmentBeanList;
	}

	/**
	 * 修改部门
	 */
	@Override
	public boolean updateDepartment(DepartmentBean departmentBean) {
		Integer departmentId=departmentBean.getDepartmentId();
		Department department=new Department();
		DepartmentExample examples=new DepartmentExample();
		DepartmentExample.Criteria criterias = examples.createCriteria();
		criterias.andDepartmentIdEqualTo(departmentId);
		Department curDepartment=DepartmentDao.selectByPrimaryKey(departmentId);
		department.setDepartmentId(departmentId);
		department.setDepartmentCode(departmentBean.getDepartmentCode());
		department.setDepartmentName(departmentBean.getDepartmentName());
		department.setDescription(departmentBean.getDescription());
		department.setCorporationId(departmentBean.getCorporationId());
		department.setOrgLevel(departmentBean.getOrgLevel());
		department.setParentDepartmentId(departmentBean.getParentDepartmentId());
		department.setFromDate(departmentBean.getFromDate());
		department.setThruDate(departmentBean.getThruDate());
		department.setIsDisable(0);
		department.setUpdateTime(new Date());
		department.setCreateTime(curDepartment.getCreateTime());
		
		if(departmentBean.getParentDepartmentId()!=null) {
			DepartmentExample example=new DepartmentExample();
			DepartmentExample.Criteria criteria = example.createCriteria();
			criteria.andDepartmentIdEqualTo(departmentBean.getParentDepartmentId());
			List<Department> list = DepartmentDao.selectByExample(example);
			department.setDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
			departmentBean.setFromDepartmentPath(curDepartment.getDepartmentPath());
			departmentBean.setToDepartmentPath(list.get(0).getDepartmentPath()+department.getDepartmentId()+"-");
			DepartmentExtDao.updateDepartmentPath(departmentBean);
		} else {
			department.setDepartmentPath("-"+department.getDepartmentId()+"-");
			departmentBean.setFromDepartmentPath(curDepartment.getDepartmentPath());
			departmentBean.setToDepartmentPath("-"+department.getDepartmentId()+"-");
			DepartmentExtDao.updateDepartmentPath(departmentBean);
		}
		int record = DepartmentDao.updateByPrimaryKey(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除一条部门数据
	 */
	@Override
	public boolean deleteDepartmentList(Integer departmentId) {
		Department department=new Department();
		department.setDepartmentId(departmentId);
		department.setIsDisable(1);
		department.setDisableTime(new Date());
		department.setUpdateTime(new Date());
		int record = DepartmentDao.updateByPrimaryKeySelective(department);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据DepartmentId获取一条数据
	 */
	@Override
	public Department getDepartmentByDepartmentId(Integer departmentId) {
		
		return DepartmentDao.selectByPrimaryKey(departmentId);
	}

	/**
	 * 根据Department签到时间判断数据库里是否有相同的数据
	 */
	@Override
	public boolean getDepartmentByDepartment(DepartmentBean departmentBean) {
		DepartmentExample example=new DepartmentExample();
		DepartmentExample.Criteria criteria = example.createCriteria();
		criteria.andDepartmentCodeEqualTo(departmentBean.getDepartmentCode());
		if (departmentBean.getDepartmentId()!=null) {
			criteria.andDepartmentIdNotEqualTo(departmentBean.getDepartmentId());
		}
		criteria.andIsDisableEqualTo(0);
		List<Department> departmentList=DepartmentDao.selectByExample(example);
		if(departmentList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 根据id获取部门
	 */
	@Override
	public List<EmployeeDepartment> getEmployeeDepartmentListByDepartmentId(Integer departmentId) {
		EmployeeDepartmentExample example = new EmployeeDepartmentExample();
		example.createCriteria().andDepartmentIdEqualTo(departmentId);
		List<EmployeeDepartment> list = EmployeeDepartmentDao.selectByExample(example);
		return list;
	}
	/**
	 * 根据公司获取部门
	 */
	@Override
	public List<Department> getDepartmentByCorporationId(Integer corporationId) {
		DepartmentExample example = new DepartmentExample();
		example.createCriteria().andCorporationIdEqualTo(corporationId);
		List<Department> list = DepartmentDao.selectByExample(example);
		return list;
	}
	/**
	 * 根据部门路径获取所属部门
	 */
	@Override
	public List<Department> getDepartmentRoidByPath(Integer departmentId) {
		DepartmentExample example = new DepartmentExample();
		example.createCriteria().andDepartmentPathLike("-"+departmentId+"-");
		List<Department> list = DepartmentDao.selectByExample(example);
		return list;
	}
}
